/*!
 * vinculum
 * Copyright(c) 2017 azzly
 * MIT Licensed
 */
 
'use strict';

/**
 * Module dependencies.
 * @private
 */
 
const helper = require('./helper');
const minify = require('minify');
const babel = require('babel-core');
const less = require('less');

/**
 * prototype.
 */

let obj = exports = module.exports = {};

/**
 * babel_js
 *
 * @public
 */
 
obj.babel_js = function(code,minified,callback){
	try{
		const out = babel.transform(code, {
			babelrc:false
			,presets: ['es2015']
			,minified:!!minified
		});
		callback(null,out.code);
	}catch(err){
		callback(err);
	}
};

/**
 * less_css
 *
 * @public
 */
 
obj.less_css = function(code,minified,callback){
	less.render(code,{
			paths: ['./templates/less'], 
			compress: !!minified
		},(e, output) =>{
			if(e){
				console.error(e);
				callback(e);
			}else{
				callback(null,output.css);
			}
	});
};

/**
 * minifyJS
 *
 * @public
 */
 
obj.minifyJS = function(data,callback){
	if(typeof data === 'string' && data.length>0){
		minify.js(data,callback);
	}else{
		callback(new Error('js data must be string'));
	}
};

/**
 * minifyCSS
 *
 * @public
 */
 
obj.minifyCSS = function(data,callback){
	if(typeof data === 'string' && data.length>0){
		minify.css(data,callback);
	}else{
		callback(new Error('css data must be string'));
	}
};

/**
 * minifyHTML
 *
 * @public
 */
 
obj.minifyHTML = function(data,callback){
	if(typeof data === 'string' && data.length>0){
		minify.html(data,callback);
	}else{
		callback(new Error('html data must be string'));
	}
};